package manage.controller;

import core.constants.HttpConstants;
import core.controller.BaseController;
import core.domain.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import manage.domain.sysuser.dto.SysLoginDTO;
import core.domain.vo.LoginUserVO;
import manage.domain.sysuser.vo.SysUserVO;
import manage.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Tag(name="系统用户管理")
@RequestMapping("/sysUser")
public class SysUserController extends BaseController {
    @Autowired
    private ISysUserService sysUserService;
    @PostMapping("/login")
    @Operation(summary = "管理员登录")
    @ApiResponse(responseCode = "200", description = "登录成功")
    @ApiResponse(responseCode = "500", description = "服务器繁忙, 请稍后再试")
    @ApiResponse(responseCode = "313", description = "用户名或密码错误")
    @ApiResponse(responseCode = "312", description = "用户名不存在")
    public Result<String> login(@RequestBody SysLoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        log.info("SysUserController  login(): {}, {}", username, password);
        return sysUserService.login(username, password);
    }

    @PostMapping("/add")
    @Operation(summary = "新增管理员")
    @ApiResponse(responseCode = "200", description = "新增成功")
    @ApiResponse(responseCode = "500", description = "服务器繁忙, 请稍后再试")
    @ApiResponse(responseCode = "311", description = "用户已存在")
    public Result<Void> add(@RequestBody SysLoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        log.info("SysUserController  add(): {}, {}", username, password);
        return toResult(sysUserService.add(username, password));
    }

    @GetMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        log.info("SysUserController  info(): {}", token);
        return sysUserService.info(token);
    }

    @GetMapping("/detail")
    @Operation(summary = "获取用户详情")
    @Parameters({
            @Parameter(name = "id", in = ParameterIn.QUERY, required = true, description = "用户ID"),
    })
    @ApiResponse(responseCode = "200", description = "获取成功")
    @ApiResponse(responseCode = "500", description = "服务器繁忙, 请稍后再试")
    @ApiResponse(responseCode = "310", description = "用户不存在")
    public Result<SysUserVO> detail(@RequestParam(required = true) Long id) {
        return null;
    }


    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        log.info("SysUserController  logout(): {}", token);
        return toResult(sysUserService.logout(token));
    }
}
